/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.*;
import BUS.*;
import TranSport.StringDataBase;
import TranSport.StringTable;
import TranSport.Transport;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class ServerListen extends  Thread{
    public ServerSocket serverSocket ;
    public Socket clientSocket;
    
    public ObjectOutputStream output;
    public ObjectInputStream input;
    
    public Transport tran;
    public boolean start ;
    public boolean isConnect;
    public String Username;
    public String Password;
    public int Port;
    
    public ServerListen (int port , String User, String Pass)
    {
        start = true;
        isConnect = false;
        tran = new Transport();
        this.Username = User;
        this.Password = Pass;
        this.Port = port;
        
        // mo port lang nghe tu client 
        try
        {
        serverSocket = new ServerSocket(port);
        }catch(Exception ex)
        {
            JFrame f = new JFrame();
            JOptionPane.showMessageDialog(f,"Loi Ko Tao dc serverSocket de lang nghe   : "+ex.getMessage());
            this.Stop_Listen();
            return;
        }
        // lang nghe tu client 
        try
        {
        clientSocket = serverSocket.accept();
        }catch(Exception ex)
        {
            JFrame f = new JFrame();
            JOptionPane.showMessageDialog(f,"Loi Ko accept    : "+ex.getMessage());
            this.Stop_Listen();
            return;
        }
        
         try
        {
        input = new ObjectInputStream(clientSocket.getInputStream());
        output = new ObjectOutputStream(clientSocket.getOutputStream());
        }catch(Exception ex)
        {
            JFrame f = new JFrame();
            JOptionPane.showMessageDialog(f,"Loi Lay cac dong nhap xuat     : "+ex.getMessage());
            this.Stop_Listen();
            return;
        }

         
        start();
    }
    
    public void run()
    {
       while(start)
       {
                   // kiem tra dang nhap
         
         try
         {
             tran.Copy((Transport)input.readObject());
             
             
             if(tran!=null)
             {
                 // truog hop login
                 if(tran.Mesage.equalsIgnoreCase("Login"))
                 {
                     // kiem tra username va pass word 
                     if(tran.login.UserName.equals(Username))
                     {
                         // Ok Username tien hanh kiem tra pass
                         if(tran.login.Password.equals(Password))
                         {
                             // Ok dung password
                             isConnect = true;
                             // thong bao connect thanh cong den cho client 
                             tran.Mesage = "LoginOK";
                             // lay danh sach database tu server do ve cho client
                             Vector<StringDataBase> V_dataBase = new Vector<StringDataBase>();
                             
                             
                             
                             DataManager data = new DataManager();
                             data.ReadDatabase();
                             String [] arrayDataBase = data.GetList_Database();
                             for(int i = 0;i<arrayDataBase.length;i++)
                             {
                                 StringDataBase stringDataBase = new StringDataBase();
                                 stringDataBase.DataBaseName = arrayDataBase[i];
                                 
                                 String [] arrayTable = data.GetList_Table(arrayDataBase[i]);
                                 
                                 StringTable[] listTable = new StringTable[arrayTable.length];
                                 
                                 for(int j = 0;j<arrayTable.length;j++)
                                 {
                                     
                                     
                                     StringTable stringTable = new StringTable();
                                     stringTable.TableName = arrayTable[j];
                                     
                                     String[] arraycolum = data.Get_List_Column(arrayDataBase[i], arrayTable[j]);
                                     
                                     stringTable.Columns = arraycolum;
                                     
                                     listTable[j] = stringTable;
                                     
                                 }
                                 
                                 stringDataBase.Table = listTable;
                                 V_dataBase.addElement(stringDataBase);
                             }
                             
                             tran.stringDataBase = V_dataBase;
                             
                             output.writeObject(tran);
                         }
                         else
                         {
                             // sai pass
                             isConnect = false;
                             tran.errorMessage = "Password ko dung ! ";
                             tran.Mesage = "LoginError";
                             output.writeObject(tran);
                         }
                        
                     }
                     else
                     {
                         // connet that bai 
                         isConnect = false;
                         
                         // guoi thong diep that bai ve cho client va lang nghe lai 
                         tran.errorMessage = "Ten Username ko dung ! ";
                         tran.Mesage = "LoginError";
                         output.writeObject(tran);
                         
                     }
                 }
                 else if (tran.Mesage.equalsIgnoreCase("Querry"))
                 {
                     if(isConnect)
                     {
                         Transport tam = new Transport();
                        BUS_ParseQuerry partQuerry = new BUS_ParseQuerry(tran.dataBaseName);
                                 try
                        {
                            
                           // tran.tableKQ = (Bus_Table)partQuerry.ParseQuerry(tran.Sql);
                             tam.tableKQ = (Bus_Table)partQuerry.ParseQuerry(tran.Sql);
                        }catch(Exception e)
                        {
                            // ket qua tra ve ko phai la table 
                            if(partQuerry.syntax_Querry)
                            {
                                // ko su ly 
                              //  tran.Mesage = "OK";
                                tam.Mesage = "OK";
                            }
                            else
                            {
                                //tran.Mesage = "Error";
                                tam.Mesage = "Error";
                            }
                            //tran.errorMessage = partQuerry.Message_Error;
                            tam.errorMessage = partQuerry.Message_Error;

                        }
                                // tran.stringDataBase = null;
                                 
                                 output.writeObject(tam);
                     }
                 }
                 else if(tran.Mesage.equals("quit"))
                 {
                     // ngung su ly voi client 
                     Stop_Listen();
                 }
                
             }
         }catch(Exception ex)
         {
             JFrame f = new JFrame();
            JOptionPane.showMessageDialog(f,"Loi tai doc du lieu tu client   : "+ex.getMessage());
            this.Stop_Listen();
            return;
         }
       }
    }
    public void Stop_Listen()
    {
        this.serverSocket = null;
        this.clientSocket = null;
        this.start = false;
        this.isConnect = false;
        this.tran =null;
        this.input = null;
        this.output = null;
    }
    public boolean CheckPortListen(int port)
    {
        if(this.Port == port)
        {
            // 
            return false;
        }
        return true;
    }
    public boolean CheckUserListen(String User)
    {
        if(this.Username.equals(User))
        {
            return false;
        }
        return true;
    }
    
}
